<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="老师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入老师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="学员名称" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="curriculumName">
        <el-input
          v-model="queryParams.curriculumName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option label="正常" value="1" ></el-option>
          <el-option label="已完成" value="2" ></el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jiaopei:scheduling:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jiaopei:scheduling:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jiaopei:scheduling:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jiaopei:scheduling:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schedulingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" v-if="true"  min-width="60"  show-overflow-tooltip />
      <el-table-column label="老师" align="center" prop="teacherVo.nickName" min-width="80"  show-overflow-tooltip />
      <el-table-column label="学员" align="center" prop="studentVo.name" min-width="80"  show-overflow-tooltip />
      <el-table-column label="课程" align="center" prop="curriculumVo.name" min-width="80"  show-overflow-tooltip />
      <el-table-column label="课时" align="center" prop="lessonHours" min-width="80"  show-overflow-tooltip />

      <el-table-column label="每周几" align="center" prop="week" min-width="80"  show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.week_type" :value="scope.row.week"/>
        </template>
      </el-table-column>
      <el-table-column label="课程时间" align="center" prop="courseDuration" min-width="98"  show-overflow-tooltip />
      <el-table-column label="开始时间" align="center" prop="startHour" min-width="80"  show-overflow-tooltip />
      <el-table-column label="结束时间" align="center" prop="endHour" min-width="80"  show-overflow-tooltip />

      <el-table-column label="备注" align="center" prop="remarks" min-width="100"  show-overflow-tooltip />
      <el-table-column label="签到状态" align="center" prop="status" min-width="80"  show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{  (scope.row.status=='1'?'正常':'已完成') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jiaopei:scheduling:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jiaopei:scheduling:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排期对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="老师" prop="teacherName">
          <el-input v-model="form.teacherName" readonly placeholder="请输入授课老师" >
            <i  class="el-icon-edit el-input__icon" slot="suffix" @click="openSelectUser" />
          </el-input>
        </el-form-item>

        <el-form-item label="学员" prop="studentName">
          <el-input v-model="form.studentName" readonly placeholder="请选择学员" >
            <i  class="el-icon-edit el-input__icon" slot="suffix" @click="openSelectStudent" />
          </el-input>
        </el-form-item>

        <el-form-item label="课程" prop="curriculumName">
          <el-input v-model="form.curriculumName" readonly placeholder="请输入课程" >
            <i  class="el-icon-edit el-input__icon" slot="suffix" @click="openSelectCurriculum" />
          </el-input>
        </el-form-item>

        <el-form-item label="课时" prop="lessonHours">
          <el-input-number  v-model="form.lessonHours"  :min="1" :max="99999"  placeholder="请输入课时"></el-input-number>
        </el-form-item>
        <el-form-item label="每周几" prop="week">
          <el-select class="form-item-input-style" v-model="form.week" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.week_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="课程时间" prop="courseDuration">
          <el-date-picker clearable
                          v-model="form.courseDuration"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择课程时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="开始时间"  prop="startHour">
          <el-time-select
            size="small"
            placeholder="起始时间"
            v-model="form.startHour"
            :picker-options="{start: '06:00',step: '00:10',end: '23:00'}">
          </el-time-select>
        </el-form-item>
        <el-form-item label="结束时间" prop="endHour">
          <el-time-select
            size="small"
            placeholder="结束时间"
            v-model="form.endHour"
            :picker-options="{start: '06:30',step: '00:10',end: '23:00',minTime:form.startHour}">
          </el-time-select>
        </el-form-item>

        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option label="正常" value="1" ></el-option>
            <el-option label="已完成" value="2" ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <select-user ref="select"  @ok="selectUserOk" />

    <select-student ref="selectStudent"  @ok="openSelectStudentOk" />

    <select-curriculum ref="selectCurriculum"  @ok="selectCurriculumOk" />


    <Dialog ref="dialog" @refresh="getList"></Dialog>
  </div>
</template>

<script>
import { listScheduling, getScheduling, delScheduling, addScheduling, updateScheduling } from "@/api/jiaopei/scheduling";
import selectStudent from "@/views/jiaopei/student/selectStudent";
import selectCurriculum from "@/views/jiaopei/class/selectCurriculum";
import selectUser from "@/views/jiaopei/class/selectUser";
import Dialog from "@/views/jiaopei/scheduling/dialog";
export default {
  name: "Scheduling",
  dicts: ['week_type'],
  components: { selectStudent,selectCurriculum ,selectUser, Dialog},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排期表格数据
      schedulingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: undefined,
        week: undefined,
        courseDuration: undefined,
        startHour: undefined,
        endHour: undefined,
        lessonHours: undefined,
        studentId: undefined,
        curriculumId: undefined,
        remarks: undefined,
        status: undefined,
        createdDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        teacherName: [
          { required: true, message: "老师不能为空", trigger: "blur" }
        ],
        lessonHours: [
          { required: true, message: "课时不能为空", trigger: "blur" }
        ],
        studentName: [
          { required: true, message: "学员不能为空", trigger: "blur" }
        ],
        curriculumName: [
          { required: true, message: "课程不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排期列表 */
    getList() {
      this.loading = true;
      listScheduling(this.queryParams).then(response => {
        this.schedulingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        teacherId: undefined,
        teacherName: undefined,
        week: undefined,
        courseDuration: undefined,
        startHour: undefined,
        endHour: undefined,
        lessonHours: 1,
        studentId: undefined,
        studentName: undefined,
        curriculumId: undefined,
        curriculumName: undefined,
        remarks: undefined,
        status: '1',
        createdDate: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.dialog.show();
      return;

      this.reset();
      this.open = true;
      this.title = "添加排期";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.dialog.show(row.id);
      return;


      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getScheduling(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改排期";
        if(this.form['teacherVo']){
          this.form.teacherName=this.form['teacherVo']['nickName'];
        }
        if(this.form['curriculumVo']){
          this.form.curriculumName=this.form['curriculumVo']['name'];
        }
        if(this.form['studentVo']){
          this.form.studentName=this.form['studentVo']['name'];
        }

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateScheduling(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addScheduling(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除排期编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delScheduling(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jiaopei/scheduling/export', {
        ...this.queryParams
      }, `scheduling_${new Date().getTime()}.xlsx`)
    },
    openSelectStudent(){
      let curriculumId=undefined;
      if(this.$route.query['curriculumId']){
        curriculumId=this.$route.query['curriculumId'];
      }
      if(this.form['curriculumId']){
        curriculumId=this.form['curriculumId'];
      }

      this.$refs.selectStudent.show(true,{'curriculumId':curriculumId});
    },
    openSelectStudentOk(row){
      this.form.studentId=row['id'];
      this.form.studentName=row['name'];
    },
    openSelectCurriculum() {
      this.$refs.selectCurriculum.show();
    },
    selectCurriculumOk(row){
      this.form.curriculumId=row['id'];
      this.form.curriculumName=row['name'];
      this.$forceUpdate();
    },
    openSelectUser() {
      this.$refs.select.show();
    },
    selectUserOk(row){
      this.form.teacherId=row['userId'];
      this.form.teacherName=row['nickName'];
      this.$forceUpdate();
    }
  }
};
</script>
